﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace SnakeGame_Roman
{


   
   public class GameEngine
    {
       private TickGenerator generator = new TickGenerator();
       private MoveGenerator movegenerator = new MoveGenerator();
       private GameRender render = new OptimizedRenderer(); 
       private BadWordGenerator badWord = new BadWordGenerator();
       private List<int> gameSpeed = new List<int>();
       private static int speed;
       //private const Int32 speed = 19;
      
       
       
       public GameField field { get; set; }
       public Food food { get; set; }
       public Snake snake1 { get; set; }
       public int _gameSpeed;
     
     
       public int x { get; set; }
       public int y { get; set; }
      

        public void Start(Snake newSnake, Food food, GameField field)
        {
            this.food = food;
            this.snake1 = newSnake;
            gameSpeed = this.generator.GameSpeed();
            speed = 0;
         
        
            generator.Tick += generator_Tick;
            generator.Start(gameSpeed[8]);
          
        }

       void generator_Tick(object sender, EventArgs e)
        {
         
         
           this.field = new GameField(40 , 20);
         
       // movegenerator.Move += movegenerator_Move;
      //  movegenerator.ChangeDirection(snake1);
        snake1.MoveSnake(snake1);
       
       
           if (snake1.piesesOfSnake[0].X == food.x & snake1.piesesOfSnake[0].Y == food.y)
           {
               Random rand = new Random();
               food.x = rand.Next(1, field.lenght-1);
               food.y = rand.Next(1, field.width - 1);
               food.foodId++;
               



             int a = snake1.lenghtOfSnake-1;
             snake1.AddNewPiese(snake1.piesesOfSnake[a].X,snake1.piesesOfSnake[a].Y);
             snake1.lenghtOfSnake++;


               if (food.foodId % 2 == 1)
                {
                    this._gameSpeed = ++speed;
                    string word = badWord.ShowNewBadWord();
                    Console.SetCursorPosition(field.lenght + 1, field.centreY + 3);
                    Console.WriteLine(word);

                }
             Console.Beep();
           }
           if (snake1.piesesOfSnake[0].X == field.lenght - 1)
           {
               snake1.piesesOfSnake[0].X = 1;
           }
           else if (snake1.piesesOfSnake[0].X == 0)
           {
               snake1.piesesOfSnake[0].X = field.lenght - 2;
           }
           else if (snake1.piesesOfSnake[0].Y == 0)
           {
               snake1.piesesOfSnake[0].Y = field.width - 2;
           }
           else if (snake1.piesesOfSnake[0].Y == field.width-1)
           {
               snake1.piesesOfSnake[0].Y = 1;

           }
               /* if (snake1.piesesOfSnake[0].X == 0 | snake1.piesesOfSnake[0].X == field.lenght-1 | snake1.piesesOfSnake[0].Y == 0 | snake1.piesesOfSnake[0].Y == field.width-1)
                {
                    End();
                }
                */
               Console.SetCursorPosition(field.lenght + 1, field.centreY);
           Console.WriteLine("Синяя змея схавала {0} зеленых кусков!",food.foodId-1);
           Console.SetCursorPosition(0, 0);
       
        
        render.RenderField(field);
        render.RenderFood(food);
        render.RenderSnake(snake1);
    


        
       }

        public void Pause()
        {

        }
        public void End()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(field.centreX, field.centreY);
            Console.Beep();
            Console.Beep();
            Console.Beep();
            Console.WriteLine("GAME OVER");
            Console.ReadLine();
            Console.ResetColor();
        }

     
      
      

        

    }
}
